package com.rjyb.student_management_platform.service.Impl;

import com.rjyb.student_management_platform.dao.AnswerDao;
import com.rjyb.student_management_platform.pojo.Class;
import com.rjyb.student_management_platform.pojo.*;
import com.rjyb.student_management_platform.service.AnswerService;
import com.rjyb.student_management_platform.unit.common.Pager;
import com.rjyb.student_management_platform.unit.load.FileUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AnswerServiceImpl implements AnswerService {
    @Autowired
    AnswerDao ad;


    @Override
    public List<Class> classid(Teacher teacher, HttpSession session, Model model) {
        String yes = (String) session.getAttribute("yes");
        if(yes!=null&&!yes.equals("")){
            model.addAttribute("yes",yes);
            session.removeAttribute("yes");
        }
        return ad.classid(teacher.getTid());
    }

    @Override
    public Booksubject selBooksubject(HttpSession session) {
        Teacher teacher = (Teacher) session.getAttribute("teacher");
        return ad.selBooksubject(teacher.getTid());
    }

    @Override
    public void addJobtable(Jobtable jobtable, Teacher teacher, HttpSession session, MultipartFile file1, MultipartFile file2, MultipartFile file3, MultipartFile file4) {
        if (jobtable.getJobName()!=null&&!jobtable.getJobName().equals("")) {
            String filerl1 = FileUpload.upload(file1,session,"file1");
            String filerl2 = FileUpload.upload(file2,session,"file2");
            String filerl3 = FileUpload.upload(file3,session,"file3");
            String filerl4 = FileUpload.upload(file4,session,"file4");
            if (filerl1==null){
                filerl1 = "undefined";
            }
            if (filerl2==null){
                filerl2 = "undefined";
            }
            if (filerl3==null){
                filerl3 = "undefined";
            }
            if (filerl4==null){
                filerl4 = "undefined";
            }
            jobtable.setJobSrc(filerl1);
            jobtable.setJobSrc1(filerl2);
            jobtable.setJobSrc2(filerl3);
            jobtable.setJobSrc3(filerl4);
            jobtable.setTid(teacher.getTid());
            System.out.println(jobtable);
            ad.addJobtable(jobtable);
            session.setAttribute("yes","发布成功");
        }else {
            session.setAttribute("yes","发布失败");
        }

    }


    public AnswerServiceImpl() {
        super();
    }

    @Override
    public List<Map> answerlist(Pager pager,Integer jid) {
        return ad.selectall(pager,jid);
    }
//有改动
    @Override
    public boolean updateWork(Integer correct, Integer remedy, Integer jid,Integer sid,String mistake) {
        return ad.updateWork(correct,remedy,jid,sid,mistake);
    }
    @Override
    public List<HashMap> selectimg(Integer jid,Integer sid) {
        return ad.selectimg(jid,sid);
    }




    @Override
    public int selcountanswer( Integer jid) {
        return ad.selstucount(jid);
    }
//判断作业做没做
    @Override
    public String ifHomeWork1(Integer jid, Integer sid) {
        return ad.ifHomeWork1(jid,sid);
    }
//判断作业改没改
    @Override
    public HashMap ifHomeWork(Integer jid,Integer sid) {
        return ad.ifHomeWork(jid,sid);
    }
    //查作业
    @Override
    public List<HashMap> selectHomeWork(Long sid) {
        return ad.selectHomeWork(sid);
    }

    //交作业
    @Override
    public void addAnswerSheet(AnswerSheet answerSheet) {
        //判断是否提交答案
        if (answerSheet.getAnswer()!=null||answerSheet.getImgsrc1()!=null||answerSheet.getImgsrc()!=null||answerSheet.getImgsrc2()!=null||answerSheet.getImgsrc3()!=null) {

            ad.addAnswerSheet(answerSheet);
        }else {
            System.out.println("答案未输入，或者图片没上传");
        }

    }
//查老师名字
    @Override
    public List<HashMap> selectTeacherName(Integer jid) {
        return ad.selectTeacherName(jid);
    }

    @Override
    public List<HashMap> selectSumHomeWork(Integer tid) {
        return ad.selectSumHomeWork(tid);
    }


    //学生拿没过的作业
    @Override
    public List<Map> TakeHomework(String aid,HttpSession session,Model model) {
        String yes = (String) session.getAttribute("no");
        if(yes!=null&&!yes.equals("")){
            model.addAttribute("no",yes);
            session.removeAttribute("no");
        }

        Student answerSheet1 = (Student) session.getAttribute("student");
        long sid = answerSheet1.getSid();


        return ad.TakeHomework(sid,aid);
    }

    //学生上传修改完的作业
    @Override
    public void UpdateAnswerSheet(HttpSession session,AnswerSheet answerSheet,MultipartFile file1,MultipartFile file2,MultipartFile file3,MultipartFile file4,Model model) {
        if (answerSheet.getAnswer()!=null&&!answerSheet.getAnswer().equals("")) {
            String filerl1 = FileUpload.upload(file1,session,"file1");
            String filerl2 = FileUpload.upload(file2,session,"file2");
            String filerl3 = FileUpload.upload(file3,session,"file3");
            String filerl4 = FileUpload.upload(file4,session,"file4");
            if (filerl1==null){
                filerl1 = "undefined";
            }
            if (filerl2==null){
                filerl2 = "undefined";
            }
            if (filerl3==null){
                filerl3 = "undefined";
            }
            if (filerl4==null){
                filerl4 = "undefined";
            }

            answerSheet.setImgsrc(filerl1);
            answerSheet.setImgsrc1(filerl2);
            answerSheet.setImgsrc2(filerl3);
            answerSheet.setImgsrc3(filerl4);

            System.out.println(filerl1+1111);
            System.out.println(filerl2);
            System.out.println(filerl3);
            System.out.println(filerl4);

            ad.UpdateAnswerSheet(answerSheet);
            session.setAttribute("no","发布成功");
        }else {
            session.setAttribute("no","发布失败");
        }

    }

}
